//
//  BaseHttpManager.m
//  ProjectForCustomer
//
//  Created by 钟游 on 2019/8/25.
//  Copyright © 2019年 钟游. All rights reserved.
//

#import "BaseHttpManager.h"
#import "AFNetworking.h"
#import<CommonCrypto/CommonDigest.h>

@implementation BaseHttpManager
+(instancetype)defaultManager;{
    static BaseHttpManager * manager = nil;
    static dispatch_once_t  onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[BaseHttpManager alloc]init];
    });
    return manager;
}
-(void)requestWitAPI:(NSString*)api requestMeth:(NSString*)meth info:(NSDictionary*)info sucess:(void (^) (NSDictionary* responseObject))success failure:(void (^)(NSString* error_))failure;{
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain",nil];
    
    NSString * rangeStr = [self rangeByCharator:info];
    NSString * token =@"";

    rangeStr=[rangeStr stringByAppendingString:token];
    NSString * signStr  =[self md5:rangeStr];
    UserInfoModel * model =[StorageManager getProperty:@"userInfo"];
    NSString * userid = @"";
    if (!model) {
        userid = @"0";
         token=@"f7f4c4e6b81d11e9";
    }else{
        userid = model.userId;
        token = model.token;
    }
    api=[SERVICE_URL stringByAppendingString:api];
//    if ([api isEqualToString:@"/common/getVerificationCode"]) {
//
//    }else{
        [manager.requestSerializer setValue:userid forHTTPHeaderField:@"movienow-userid"];
        [manager.requestSerializer setValue:@"4" forHTTPHeaderField:@"platform"];
        [manager.requestSerializer setValue:@"1" forHTTPHeaderField:@"version"];
        [manager.requestSerializer setValue:signStr forHTTPHeaderField:@"sign"];
        [manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
 //  }
 
    
    if ([meth isEqualToString:@"POST"]) {
        [manager POST:api parameters:info progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            success(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failure(error.description);
        }];
    }else{
        [manager GET:api parameters:info progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            success(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failure(error.description);
        }];
    }
   
}


#pragma mark -- MD5
- (NSString *) md5:(NSString *) input {
    
    const char *cStr = [input UTF8String];
    
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    
    CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call
    
    
    
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    
    
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        
        [output appendFormat:@"%02x", digest[i]];
    
    
    
    return  output;
    
}
#pragma mark 参数排序处理
-(NSString*)rangeByCharator:(NSDictionary*)dataDict{
    NSArray * orginalArray = [dataDict allKeys];
    NSArray * changeArray =[orginalArray sortedArrayUsingSelector:@selector(compare:)];
    
    NSString * str =@"";
    for (int i = 0 ; i<changeArray.count; i++) {
        str=[str stringByAppendingString:[NSString stringWithFormat:@"%@=%@",changeArray[i],dataDict[changeArray[i]]]];
        str=[str stringByAppendingString:@"&"];
    }
    if ([str hasSuffix:@"&"]) {
        str = [str substringWithRange:NSMakeRange(0, str.length-1)];
    }
    
    return str;
}
@end
